cmake_minimum_required(VERSION 3.15.2)
# 设置项目名称与语言
project(GCTL VERSION 1.0)
# 添加配置配件编写的函数
include(CMakePackageConfigHelpers)

# 添加编译选项
option(GCTL_OPENMP "Use the OpenMP library" ON)
option(GCTL_NETCDF "Use the NetCDF library" ON)
option(GCTL_WAVELIB "Use the WaveLib library" ON)
option(GCTL_FFTW3 "Use the FFTW3 library" ON)
option(GCTL_EEMD "Use the EEMD library" ON)
option(GCTL_OPENBLAS "Use the Openblas library" OFF)
option(GCTL_CHECK_BOUNDER "Check array's index" OFF)
option(GCTL_CHECK_SIZE "Check array's size" OFF)
option(GCTL_EXAMPLES "Compile examples" OFF)

message(STATUS "Platform: " ${CMAKE_HOST_SYSTEM_NAME})
message(STATUS "Install prefix: " ${CMAKE_INSTALL_PREFIX})
message(STATUS "Processor: " ${CMAKE_HOST_SYSTEM_PROCESSOR})
message(STATUS "[GCTL] Use the OpenMP library: " ${GCTL_OPENMP})
message(STATUS "[GCTL] Use the NetCDF library: " ${GCTL_NETCDF})
message(STATUS "[GCTL] Use the WaveLib library: " ${GCTL_WAVELIB})
message(STATUS "[GCTL] Use the FFTW3 library: " ${GCTL_FFTW3})
message(STATUS "[GCTL] Use the EEMD library: " ${GCTL_EEMD})
message(STATUS "[GCTL] Use the Openblas library: " ${GCTL_OPENBLAS})
message(STATUS "[GCTL] Check Bounder: " ${GCTL_CHECK_BOUNDER})
message(STATUS "[GCTL] Check Size: " ${GCTL_CHECK_SIZE})

if(GCTL_FFTW3)
	if(NOT FFTW3_FOUND)
		find_package(FFTW3 REQUIRED)
		message(STATUS "Found FFTW3")
		include_directories(${FFTW3_INC_DIR})
	endif()
endif()

if(GCTL_EEMD)
	if(NOT EEMD_FOUND)
		find_package(EEMD REQUIRED)
		message(STATUS "Found EEMD")
		include_directories(${EEMD_INC_DIR})
	endif()
endif()

if(GCTL_OPENBLAS)
	if(NOT OpenBLAS_FOUND)
		find_package(OpenBLAS REQUIRED)
		message(STATUS "Found OpenBLAS")
		include_directories(${OpenBLAS_INCLUDE_DIRS})
	endif()
endif()

if(GCTL_NETCDF)
	if(NOT netCDF_FOUND)
		find_package(netCDF REQUIRED)
		include_directories(${netCDF_INCLUDE_DIR})
	endif()

	if(NOT netCDF_CXX_LEGACY_FOUND)
		find_package(netCDF_CXX_LEGACY REQUIRED)
		include_directories(${netCDF_CXX_LEGACY_INC_DIR})
	endif()

	message(STATUS "Found NetCDF")
endif()

if(GCTL_WAVELIB)
	if(NOT WaveLib_FOUND)
		find_package(WaveLib REQUIRED)
		message(STATUS "Found WaveLib")
		include_directories(${WaveLib_INC_DIR})
	endif()
endif()

if(GCTL_OPENMP)
	if(NOT OpenMP_CXX_FOUND)
		find_package(OpenMP REQUIRED)
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
		set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
		include_directories(${OpenMP_CXX_INCLUDE_DIRS})
	endif()
endif()

# 加入一个头文件配置，让cmake对源码进行操作
configure_file(
	"${PROJECT_SOURCE_DIR}/config.h.in"
	"${PROJECT_SOURCE_DIR}/lib/gctl_config.h"
	)

# 添加库源文件地址
add_subdirectory(lib)
if(GCTL_EXAMPLES)
	add_subdirectory(examples)
endif()